package vip.liux.contracts.models.pay;

import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;
import vip.liux.contracts.infrastructure.pay.PayClientConfig;
import vip.liux.contracts.shared.enums.Status;
import vip.liux.contracts.shared.enums.pay.PayChannelCode;
import vip.liux.core.utils.JsonUtils;
import vip.liux.domain.entities.auditing.AuditedEntity;

@Getter
@Setter
@Entity
@Table(name = "pay_channel")
public class PayChannel extends AuditedEntity<Long> {
    /**
     * 渠道编码
     *
     * @see PayChannelCode
     */
    private String code;
    /**
     * 状态
     *
     * @see Status
     */
    private Status status;

    /**
     * 渠道费率，单位：百分比
     */
    private Double feeRate;

    /**
     * 备注
     */
    private String remark;

    /**
     * 应用编号
     *
     * @see PayApp#getId()
     */
    private Long appId;

    /**
     * 支付渠道配置
     */
    @Convert(converter = PayClientConfigConverter.class)
    private PayClientConfig config;

    public static class PayClientConfigConverter implements AttributeConverter<PayClientConfig, String> {
        public String convertToDatabaseColumn(PayClientConfig attribute) {
            return JsonUtils.toJson(attribute);
        }

        public PayClientConfig convertToEntityAttribute(String dbData) {
            return JsonUtils.toObject(dbData, PayClientConfig.class);
        }
    }

}
